﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM.Common;
using RM.DBUtility;
namespace RM.DAL
{
    public partial class RMLog
    {

        public RMLog()
        { }
        /// <summary>
        /// 获取数据数
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) as H from X_Log");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Model.RMLog GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,datetime,thread,loglevel,logger,message,address");
            strSql.Append(" from X_Log");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)
                                      };
            parameters[0].Value = id;
            Model.RMLog model = new Model.RMLog();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.Thread = ds.Tables[0].Rows[0]["thread"].ToString();
                model.LogLevel = ds.Tables[0].Rows[0]["loglevel"].ToString();
                model.Logger = ds.Tables[0].Rows[0]["logger"].ToString();
                model.Message = ds.Tables[0].Rows[0]["message"].ToString();
                model.Address = ds.Tables[0].Rows[0]["address"].ToString();
                model.Datetime = DateTime.Parse(ds.Tables[0].Rows[0]["datetime"].ToString());
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,datetime,thread,loglevel,logger,message,address");
            strSql.Append(" from X_Log");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM X_Log");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        /// <summary>
        /// 增加一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Model.RMLog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into X_Log(");
            strSql.Append("datetime,thread,loglevel,logger,message,address)");
            strSql.Append(" values (");
            strSql.Append("@datetime,@thread,@loglevel,@logger,@message,@address)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters ={
                                          new SqlParameter("@datetime",SqlDbType.DateTime),
                                          new SqlParameter("@thread",SqlDbType.VarChar),
                                          new SqlParameter("@loglevel",SqlDbType.VarChar),
                                          new SqlParameter("@logger",SqlDbType.VarChar),
                                          new SqlParameter("@message",SqlDbType.VarChar),
                                          new SqlParameter("@address",SqlDbType.VarChar)
                                      };
            parameters[0].Value = model.Datetime;
            parameters[1].Value = model.Thread;
            parameters[2].Value = model.LogLevel;
            parameters[3].Value = model.Logger;
            parameters[4].Value = model.Message;
            parameters[5].Value = model.Address;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 删除dayCount天前的数据
        /// </summary>
        public int Delete(int dayCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from X_Log");
            strSql.Append(" where DATEDIFF(day,datetime,getdate()) > " + dayCount);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

    }
}
